package com.example.elmmerchants.Product_Management;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Update_Attributes_Page extends AppCompatActivity {

    LinearLayout attributes_item, add_attributes;
    TextView save, delete_all_attributes;

    ArrayList<String> attributes = new ArrayList<>();
    List<View> main_view = new ArrayList<>();
    List<Attributes_List> attributes_temp = new ArrayList<>();
    Map<View, EditText> has_input = new HashMap<>();
    Map<View, Integer> how_many_label = new HashMap<>();
    boolean can_upload = true;

    private class Loading_Attributes_Thread extends Thread {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    int i = 0;
                    while (i < attributes.size()) {
                        Attributes_List s = new Attributes_List();
                        View view1 = LayoutInflater.from(Update_Attributes_Page.this).inflate(R.layout.set_attributes_view, null);
                        main_view.add(view1);
                        TextView delete_view = view1.findViewById(R.id.delete_view);
                        delete_view.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                main_view.remove(view1);
                                for (int j = 0; j < attributes_temp.size(); j++) {
                                    if (attributes_temp.get(j).main_view == view1) {
                                        attributes_temp.remove(j);
                                    }
                                }
                                has_input.remove(view1);
                                how_many_label.remove(view1);
                                attributes_item.removeView(view1);
                            }
                        });
                        EditText set_attributes_name = view1.findViewById(R.id.set_attributes_name);
                        set_attributes_name.setText(attributes.get(i));
                        s.main_view = view1;
                        s.edit_text_view = view1;
                        s.label = set_attributes_name;
                        attributes_temp.add(s);
                        has_input.put(view1, set_attributes_name);
                        how_many_label.put(view1, 0);
                        EditText set_label_name = view1.findViewById(R.id.set_label_name);
                        ImageView add_button = view1.findViewById(R.id.add_button);
                        LinearLayout label_item = view1.findViewById(R.id.label_item);
                        for (int k = i + 1; k < attributes.size(); k++) {
                            if (attributes.get(k).equals("属性添加完毕")) {
                                i = k + 1;
                                break;
                            }
                            else {
                                Attributes_List label_list = new Attributes_List();
                                View view2 = LayoutInflater.from(Update_Attributes_Page.this).inflate(R.layout.attrivutes_label_view, null);
                                TextView label_name = view2.findViewById(R.id.label_name);
                                label_name.setText(attributes.get(k));
                                label_list.main_view = view1;
                                label_list.edit_text_view = view2;
                                label_list.label = label_name;
                                attributes_temp.add(label_list);
                                ImageView delete_label = view2.findViewById(R.id.delete_label);
                                how_many_label.replace(view1, how_many_label.get(view1) + 1);
                                delete_label.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        for (int i = 0; i < attributes_temp.size(); i++) {
                                            if (attributes_temp.get(i).main_view == view1 && attributes_temp.get(i).edit_text_view == view2) {
                                                attributes_temp.remove(i);
                                            }
                                        }
                                        label_item.removeView(view2);
                                        how_many_label.replace(view1, how_many_label.get(view1) - 1);
                                    }
                                });
                                label_item.addView(view2);
                                set_label_name.setText("");
                            }
                        }
                        add_button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Attributes_List s = new Attributes_List();
                                if (set_label_name.getText().toString().isEmpty()) {
                                    Toast.makeText(Update_Attributes_Page.this, "请添加属性标签", Toast.LENGTH_SHORT).show();
                                }
                                else {
                                    View view2 = LayoutInflater.from(view.getContext()).inflate(R.layout.attrivutes_label_view, null);
                                    TextView label_name = view2.findViewById(R.id.label_name);
                                    label_name.setText(set_label_name.getText().toString());
                                    s.main_view = view1;
                                    s.edit_text_view = view2;
                                    s.label = label_name;
                                    attributes_temp.add(s);
                                    ImageView delete_label = view2.findViewById(R.id.delete_label);
                                    how_many_label.replace(view1, how_many_label.get(view1) + 1);
                                    delete_label.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            for (int i = 0; i < attributes_temp.size(); i++) {
                                                if (attributes_temp.get(i).main_view == view1 && attributes_temp.get(i).edit_text_view == view2) {
                                                    attributes_temp.remove(i);
                                                }
                                            }
                                            label_item.removeView(view2);
                                            how_many_label.replace(view1, how_many_label.get(view1) - 1);
                                        }
                                    });
                                    label_item.addView(view2);
                                    set_label_name.setText("");
                                }
                            }
                        });
                        attributes_item.addView(view1);
                    }
                }
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_attributes_page);

        attributes = getIntent().getStringArrayListExtra("attributes");

        attributes_item = findViewById(R.id.attributes_item);
        add_attributes = findViewById(R.id.add_attributes);
        save = findViewById(R.id.save);
        delete_all_attributes = findViewById(R.id.delete_all_attributes);

        Loading_Attributes_Thread loadingAttributesThread = new Loading_Attributes_Thread();
        loadingAttributesThread.start();

        add_attributes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Attributes_List s = new Attributes_List();
                View view1 = LayoutInflater.from(Update_Attributes_Page.this).inflate(R.layout.set_attributes_view, null);
                main_view.add(view1);
                TextView delete_view = view1.findViewById(R.id.delete_view);
                delete_view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        main_view.remove(view1);
                        for (int j = 0; j < attributes_temp.size(); j++) {
                            if (attributes_temp.get(j).main_view == view1) {
                                attributes_temp.remove(j);
                            }
                        }
                        has_input.remove(view1);
                        how_many_label.remove(view1);
                        attributes_item.removeView(view1);
                    }
                });
                EditText set_attributes_name = view1.findViewById(R.id.set_attributes_name);
                s.main_view = view1;
                s.edit_text_view = view1;
                s.label = set_attributes_name;
                attributes_temp.add(s);
                has_input.put(view1, set_attributes_name);
                how_many_label.put(view1, 0);
                EditText set_label_name = view1.findViewById(R.id.set_label_name);
                ImageView add_button = view1.findViewById(R.id.add_button);
                LinearLayout label_item = view1.findViewById(R.id.label_item);
                add_button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Attributes_List s = new Attributes_List();
                        if (set_label_name.getText().toString().isEmpty()) {
                            Toast.makeText(Update_Attributes_Page.this, "请添加属性标签", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            View view2 = LayoutInflater.from(view.getContext()).inflate(R.layout.attrivutes_label_view, null);
                            TextView label_name = view2.findViewById(R.id.label_name);
                            label_name.setText(set_label_name.getText().toString());
                            s.main_view = view1;
                            s.edit_text_view = view2;
                            s.label = label_name;
                            attributes_temp.add(s);
                            ImageView delete_label = view2.findViewById(R.id.delete_label);
                            how_many_label.replace(view1, how_many_label.get(view1) + 1);
                            delete_label.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    for (int i = 0; i < attributes_temp.size(); i++) {
                                        if (attributes_temp.get(i).main_view == view1 && attributes_temp.get(i).edit_text_view == view2) {
                                            attributes_temp.remove(i);
                                        }
                                    }
                                    label_item.removeView(view2);
                                    how_many_label.replace(view1, how_many_label.get(view1) - 1);
                                }
                            });
                            label_item.addView(view2);
                            set_label_name.setText("");
                        }
                    }
                });
                attributes_item.addView(view1);
            }
        });

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                attributes.clear();
                can_upload = true;
                for (int i = 0; i < main_view.size(); i++) {
                    if (how_many_label.get(main_view.get(i)) == 0 || has_input.get(main_view.get(i)).getText().toString().isEmpty()) {
                        can_upload = false;
                    }
                }
                if (can_upload && !main_view.isEmpty()) {
                    for (int i = 0; i < main_view.size(); i++) {
                        for (int j = 0; j < attributes_temp.size(); j++) {
                            if (attributes_temp.get(j).main_view == main_view.get(i)) {
                                attributes.add(attributes_temp.get(j).label.getText().toString());
                            }
                        }
                        attributes.add("属性添加完毕");
                    }
                    for (int i = 0; i < attributes.size(); i++) {
                        System.out.println(attributes.get(i));
                    }
                    Intent intent = new Intent(Update_Attributes_Page.this, Update_Product_Data_Page.class);
                    intent.putStringArrayListExtra("attributes", attributes);
                    setResult(2, intent);
                    finish();
                }
                else {
                    Toast.makeText(Update_Attributes_Page.this, "请添加标签/输入属性名称", Toast.LENGTH_SHORT).show();
                }
            }
        });

        delete_all_attributes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                builder.setCancelable(false);
                builder.setTitle("是否清空");
                builder.setMessage("是否清空所有属性");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        attributes.clear();
                        Intent intent = new Intent(Update_Attributes_Page.this, Update_Product_Data_Page.class);
                        intent.putStringArrayListExtra("attributes", attributes);
                        setResult(2, intent);
                        finish();
                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });

                builder.create();
                builder.show();
            }
        });
    }
}